package com.qdsgjsfk.vision.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.base.BaseApplication;
import com.qdsgjsfk.vision.util.ToastUtil;
import com.rest.util.HttpDigestUtils;

import butterknife.BindView;
import butterknife.OnClick;
import util.PreferencesHelper;


/**
 * @Description: java类作用描述
 * @Author: lzp
 * @CreateDate: 2022/10/12 16:37
 * @UpdateRemark: 更新说明
 */
public class InputIpActivity extends BaseActivity {

    @BindView(R.id.et_ip)
    EditText et_ip;
    @BindView(R.id.tv_connect_device)
    TextView tv_connect_device;


    @Override
    protected void initViews() {
        tv_connect_device.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(et_ip.getText().toString())) {
                getScreenData(et_ip.getText().toString());
            } else {
                ToastUtil.showToast(this, "请填写设备的IP地址");
            }
        });
    }


    /**
     * 手持筛查仪
     */
    void getScreenData(String host) {
        try {
            HttpDigestUtils httpDigestUtils = new HttpDigestUtils(this);
            httpDigestUtils.setOnCallback(new HttpDigestUtils.CallBackResponse() {
                @Override
                public void onSuccess(String response) {
                    BaseApplication.isHandheldScreenerConnected = true;
                    PreferencesHelper.getInstance().set(mContext, "machineIP", host);
                    runOnUiThread(() -> ToastUtil.showToast(mContext, "连接成功"));
                    finish();
                }

                @Override
                public void onFail() {
                    runOnUiThread(() -> ToastUtil.showToast(mContext, "请确保设备与手机连接同一个wifi(同一个局域网）"));
                }
            });
            httpDigestUtils.getScreenData("https://" + host + "/db/SpotResultsExtended.csv");
        } catch (Exception e) {
        }
    }

    @Override
    protected void initData() {
        String machineIP = PreferencesHelper.getInstance().getString(this, "machineIP");
        et_ip.setText(machineIP);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_input_ip;
    }

    @OnClick(R.id.btn_cancel)
    void btn_cancel() {
        GoBack();
    }

    @OnClick(R.id.btn_back)
    void btn_Back() {
        GoBack();
    }

    private void GoBack() {
        if (BaseApplication.lastActivity == 0) {
            startActivity(TypeActivity.class);
            finish();
        } else if (BaseApplication.lastActivity == 1) {
            Intent intent = new Intent(InputIpActivity.this, StudentDetailActivity.class);
            intent.putExtra("studentId", BaseApplication.studentId);
            intent.putExtra("taskId", BaseApplication.taskId);
            startActivity(intent);
            finish();
        } else if (BaseApplication.lastActivity == 2) {
            Intent intent = new Intent(InputIpActivity.this, StudentDetailOtherActivity.class);
            intent.putExtra("studentId", BaseApplication.studentId);
            intent.putExtra("taskId", BaseApplication.taskId);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        GoBack();
    }
}
